// import { Random } from 'mockjs'
// import Mock from 'mockjs'
// import { flatten, arrayToTree } from '../common-mock-method.js'
import dayjs from 'dayjs'

let pageData = {
    list: [
        {
            id: 3,
            permissionName: '系统管理',
            permissionType: 'MENU',
            parentId: 0,
            sort: 2,
            permissionKey: null,
            permissionPath: '/system',
            permissionTopPath: null,
            icon: null,
            children: [
                {
                    id: 5,
                    permissionName: '用户管理',
                    permissionType: 'MENU',
                    parentId: 3,
                    sort: 1,
                    permissionKey: null,
                    permissionPath: '/user',
                    permissionTopPath: '/system',
                    icon: 'User',
                    children: null
                },
                {
                    id: 4,
                    permissionName: '角色管理',
                    permissionType: 'MENU',
                    parentId: 3,
                    sort: 2,
                    permissionKey: null,
                    permissionPath: '/role',
                    permissionTopPath: '/system',
                    icon: 'Key',
                    children: null
                },
                {
                    id: 6,
                    permissionName: '菜单与权限',
                    permissionType: 'MENU',
                    parentId: 3,
                    sort: 3,
                    permissionKey: null,
                    permissionPath: '/menuPermit',
                    permissionTopPath: '/system',
                    icon: null,
                    children: null
                },
                {
                    id: 7,
                    permissionName: '系统字典',
                    permissionType: 'MENU',
                    parentId: 3,
                    sort: 4,
                    permissionKey: null,
                    permissionPath: '/params',
                    permissionTopPath: '/system',
                    icon: 'Collection',
                    children: [
                        {
                            id: 8,
                            permissionName: '编辑按钮',
                            permissionType: 'BUTTON',
                            parentId: 7,
                            sort: null,
                            permissionKey: 'sys:params:edit',
                            permissionPath: null,
                            permissionTopPath: null,
                            icon: null,
                            children: null
                        },
                        {
                            id: 9,
                            permissionName: '删除按钮',
                            permissionType: 'BUTTON',
                            parentId: 7,
                            sort: null,
                            permissionKey: 'sys:params:del',
                            permissionPath: null,
                            permissionTopPath: null,
                            icon: null,
                            children: null
                        }
                    ]
                }
            ]
        },
        {
            id: 17,
            permissionName: '广场',
            permissionType: 'MENU',
            parentId: 0,
            sort: 100,
            permissionKey: null,
            permissionPath: '/playground',
            permissionTopPath: null,
            icon: null,
            children: [
                {
                    id: 18,
                    permissionName: '测试模块',
                    permissionType: 'MENU',
                    parentId: 17,
                    sort: 1,
                    permissionKey: null,
                    permissionPath: '/testView',
                    permissionTopPath: '/playground',
                    icon: null,
                    children: [
                        {
                            id: 35,
                            permissionName: '你好',
                            permissionType: 'BUTTON',
                            parentId: 18,
                            sort: null,
                            permissionKey: ':tset:btn',
                            permissionPath: null,
                            permissionTopPath: null,
                            icon: null,
                            children: null
                        },
                        {
                            id: 19,
                            permissionName: '测试表单',
                            permissionType: 'MENU',
                            parentId: 18,
                            sort: 3,
                            permissionKey: null,
                            permissionPath: '/testView/formTest',
                            permissionTopPath: '/playground',
                            icon: 'isForm',
                            children: null
                        }
                    ]
                }
            ]
        }
    ]
}

module.exports = [
    {
        url: '/mock/sys/menuPermit/tree',
        method: 'post',
        response: () => {
            return {
                code: 200,
                data: pageData.list,
                message: 'OK'
            }
        }
    },
    {   // 保存
        url: '/mock/sys/menuPermit/save',
        method: 'post',
        response: (config) => {
            const requestBody = config.body
            let index = -1
            // 递归获取目标id,判断是否能修改
            const recursiveEdit = (tree) => {
                for (let i = 0; i < tree.length; i++) {
                    if ( !config.body?.parentId && !config.body.id) {
                        // 两个id都不存在，说明是新增顶级
                        requestBody.id = Math.floor(Math.random() * 1000)
                        requestBody.createTime = dayjs().format('YYYY-MM-DD HH:mm:ss')
                        pageData.list.unshift(config.body)
                        index = tree[i].id
                        break
                    } else if (tree[i].id === config.body?.parentId && !config.body?.id) {
                        // config存在parentId，且没有id，说明是新增子级
                        requestBody.id = Math.floor(Math.random() * 1000)
                        requestBody.createTime = dayjs().format('YYYY-MM-DD HH:mm:ss')
                        if ( !tree[i].children) tree[i].children = []
                        tree[i].children.push(requestBody)
                        index = tree[i].id
                        break
                    } else if (tree[i].id === config.body?.id) {
                        // config存在id，且与tree id相同，说明是修改
                        tree[i] = requestBody
                        index = tree[i].id
                        break
                    } else if (tree[i]?.children?.length) {
                        // 选不上，继续递归
                        recursiveEdit(tree[i].children)
                    }
                }
            }
            recursiveEdit(pageData.list)
            // let result='test'
            //
            // // 把树改为一维数组再进行修改
            // let flatArray = flatten(pageData.list)
            // if ( !requestBody.id && !requestBody.parentId) {
            //     result='新增'
            //     // 两个id都不存在，说明是新增顶级
            //     requestBody.id = Math.floor(Math.random() * 1000)
            //     requestBody.children = []
            //     requestBody.createTime = new Date().getFullYear() + '-' + (+new Date().getMonth() + 1) + '-' + new Date().getDate()
            //     pageData.list.unshift(requestBody)
            // } else {
            //     if (requestBody.id) {
            //         result='修改'
            //         // 存在id，说明是修改
            //         for (let i = 0; i < flatArray.length; i++) {
            //             if(flatArray[i].id === requestBody.id){
            //                 flatArray[i]=requestBody
            //                 result={'修改':flatArray[i]}
            //                 break
            //             }
            //         }
            //     } else if ( !requestBody.id && (requestBody.parentId||requestBody.parentId===0)) {
            //         result='新增子级'
            //         // 不能存在id，但存在parentId，新增子级
            //         requestBody.id = Math.floor(Math.random() * 1000)
            //         requestBody.children = []
            //         requestBody.createTime = new Date().getFullYear() + '-' + (+new Date().getMonth() + 1) + '-' + new Date().getDate()
            //         flatArray.push(requestBody)
            //     }else{
            //         result='啥也不是'
            //
            //     }
            //     // 把修改后的一维数组转回树
            //     pageData.list = arrayToTree(flatArray)
            // }
            // {result:result,list:flatArray}
            return {
                code: 200,
                data: {type: result},
                message: '保存成功'
            }
            // if (index > -1) {
            //
            // } else {
            //     return {
            //         code: 500,
            //         data: pageData.list,
            //         message: '保存失败！'
            //     }
            // }
        }
    },
    { // 删除接口
        url: RegExp('/mock/sys/menuPermit/del/' + '.*'),
        method: 'post',
        response: (config) => {
            const id = config.url.replace('/mock/sys/menuPermit/del/', '')
            const index = pageData.list.findIndex(item => item.id === +id)
            if (index > -1) {
                if (+id === 3 || +id === 17) {
                    return {
                        code: 500,
                        data: '',
                        message: '系统基础数据，禁止删除！'
                    }
                } else {
                    pageData.list.splice(index, 1)
                    return {
                        code: 200,
                        data: id,
                        message: '删除成功！'
                    }
                }
            } else {
                return {
                    code: 500,
                    data: id,
                    message: '删除失败，未找到该数据！'
                }
            }
        }
    }
]
// export const test = () => {
//     console.log('flattenArr',)
// }
